//
//  ComposeViewController.m
//  SinaWebo
//
//  Created by 亦心 on 15/4/7.
//  Copyright (c) 2015年 亦心. All rights reserved.
//

#import "ComposeIdeaViewController.h"
#import "MyTextView.h"
#import "UserPerfenceUtil.h"
#import "Account.h"
#import "AFNetworking.h"
#import "ComposeToolbar.h"
#import "ComposePhotosView.h"
#import "SendStatusRequest.h"
#import "SendStatusService.h"
#import "FromData.h"
#import "EmotionView.h"

@interface ComposeIdeaViewController ()<UITextViewDelegate,ComposeToolbarDelegatge,UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@property (nonatomic, weak) MyTextView *textView;
@property (weak, nonatomic) ComposeToolbar *toolBar;
@property (weak, nonatomic) ComposePhotosView *photosView;
@property (strong, nonatomic) EmotionView *emotionView;
@end

@implementation ComposeIdeaViewController

- (EmotionView *)emotionView{
    if (_emotionView == nil) {
        _emotionView = [[EmotionView alloc] init];
        _emotionView.frame = CGRectMake(0, 0, self.view.width, 200);
    }
    return _emotionView;
}

-(void)viewDidLoad{
    [super viewDidLoad];
    //设置TextView
    [self initTextView];
    
    //添加ComposePhotosView
    [self initComposePhotosView];
    
    //设置工具条
    [self initToolBar];
    
}

#pragma mark 添加ComposePhotosView
-(void)initComposePhotosView{
    ComposePhotosView *photosView=[ComposePhotosView composePhotosView];
    CGFloat photosViewX=0;
    CGFloat photosViewY=100;
    CGFloat photosViewW=self.textView.frame.size.width;
    CGFloat photosViewH=self.textView.frame.size.height;
    photosView.frame=CGRectMake(photosViewX, photosViewY, photosViewW, photosViewH);
    [self.textView addSubview:photosView];
    _photosView = photosView;
}


#pragma mark 设置TextView
-(void)initTextView{
    MyTextView *textView=[[MyTextView alloc]init];
    textView.frame=self.view.bounds;
    textView.placeholder=@"分享新鲜事";
    
    //设置垂直方向可以拖拽
    textView.alwaysBounceVertical=YES;
    textView.delegate=self;
    [self.view addSubview:textView];
    self.textView=textView;
    
    //监听TextView文字改变通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:textView];
    
    //监听键盘通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
    [textView becomeFirstResponder];
}

#pragma mark 键盘即将显示
-(void)keyboardWillShow:(NSNotification *)notifice{
    CGRect keyboardF=[notifice.userInfo[UIKeyboardFrameEndUserInfoKey]CGRectValue];
    NSNumber *curve = notifice.userInfo[UIKeyboardAnimationCurveUserInfoKey];
    //取出键盘动画执行的时间
    CGFloat duration=[notifice.userInfo[UIKeyboardAnimationDurationUserInfoKey]doubleValue];
    [UIView animateWithDuration:duration animations:^{
        [UIView setAnimationCurve:curve.integerValue];
        self.toolBar.transform=CGAffineTransformMakeTranslation(0, -keyboardF.size.height);
    }];
}


#pragma mark 键盘即将退出
-(void)keyboardWillHide:(NSNotification *)notifice{
    //取出键盘动画执行的时间
    NSNumber *curve = notifice.userInfo[UIKeyboardAnimationCurveUserInfoKey];
    CGFloat duration=[notifice.userInfo[UIKeyboardAnimationDurationUserInfoKey]doubleValue];
    [UIView animateWithDuration:duration animations:^{
        [UIView setAnimationCurve:curve.integerValue];
        self.toolBar.transform=CGAffineTransformIdentity;
    }];
}

#pragma mark 设置工具条
-(void)initToolBar{
    ComposeToolbar *toolBar=[ComposeToolbar composeToolBar];
    CGFloat toolBarW=self.view.frame.size.width;
    CGFloat toolBarH=44;
    CGFloat toolBarX=0;
    CGFloat toolBarY=self.view.frame.size.height-toolBarH;
    toolBar.frame=CGRectMake(toolBarX, toolBarY, toolBarW, toolBarH);
    toolBar.delegate=self;
    [self.view addSubview:toolBar];
    self.toolBar=toolBar;
}

#pragma mark 监听TextView文字改变
-(void)textDidChange{
    self.navigationItem.rightBarButtonItem.enabled = self.textView.text.length;
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationItem.rightBarButtonItem.enabled = NO;
}

#pragma arguments UITextViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //退出键盘
    [self.view endEditing:YES];
}

#pragma ComposeToolbar代理 
-(void)composeToobar:(ComposeToolbar *)toolbar didClickdButton:(ComposeToolbarButtonType)buttonType{
    switch (buttonType) {
        case ComposeToolbarButtonTypeCamera://相机
            NSLog(@"camera");
            break;
            
        case ComposeToolbarButtonTypePicture://相册
            [self openPhotoLibrary];
            break;
        
        case ComposeToolbarButtonTypeMention://#
            
            break;
        case ComposeToolbarButtonTypeTrend://@
            
            break;
            
        case ComposeToolbarButtonTypeEmotion://😌
            [self openEmotion];
            break;
    }
}

#pragma mark Emotion
- (void)openEmotion{
    [self.textView resignFirstResponder];
    if (self.textView.inputView == nil) {
        self.textView.inputView = self.emotionView;
        __weak typeof(self)weakSelf = self;
        [self.emotionView setBlock:^(EmotionModel *emotion){
            weakSelf.textView.emotion = emotion;
        }];
    }else{
        self.textView.inputView = nil;
    }
    [self.textView becomeFirstResponder];
}


#pragma mark 打开相册
-(void)openPhotoLibrary{
    UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
    ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    ipc.delegate = self;
    [self presentViewController:ipc animated:YES completion:nil];
}

#pragma mark - 图片选择控制器的代理
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    // 1.销毁picker控制器
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    // 2.去的图片
    UIImage *image = info[UIImagePickerControllerOriginalImage];
    [self.photosView addImage:image];
}

#pragma mark 发送微博数据
-(void)send{
    SendStatusRequest *request = [SendStatusRequest sendStautsRequest];
    request.status = self.textView.emotionStr;
    //发送有图片的
    if ([_photosView getTotolImage].count) {
        NSMutableArray *fromDataArray = [NSMutableArray array];
        for (UIImage *img in [_photosView getTotolImage]) {
            FromData *fromData = [FromData fromData];
            fromData.data = UIImageJPEGRepresentation(img, 0.000001);
            fromData.name = @"pic";
            fromData.mimeType = @"image/jpeg";
            fromData.filename = [NSString stringWithFormat:@"%d.jpeg", arc4random_uniform(1000)];
            [fromDataArray addObject:fromData];
        }
        [SendStatusService sentStatusImageWith:request fromDataArray:fromDataArray success:^(SendStatusResponse *response) {
//            [MBProgressHUD showSuccess:@"发送成功"];
        } error:nil];
    }else{
        [SendStatusService sentStatusWith:request success:^(SendStatusResponse *response) {
//            [MBProgressHUD showSuccess:@"发送成功"];
        } error:nil];
    }
    
    
    [self cancel];
}

-(void)dealloc{
    //移除监听器
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
